using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.IO;
using CodePlex.Resourceful.Metaweb;
using CodePlex.Resourceful.Json;
using System.Diagnostics;

namespace CodePlex.Resourceful.Examples
{
    public static class MetawebExample
    {


      
        public static void Run()
        {

            // metaweb/freebase api
            // http://freebase.com/view/helptopic?id=%239202a8c04000641f800000000544e14d


            // get beta cookie value (see pg 63 of the api doc)
            string metawebUser = Environment.GetEnvironmentVariable("METAWEB_USER");


            MetawebClient client = new MetawebClient(metawebUser);


            //GetDomains(client);
            GetFirstTenRadioheadSingles(client);
            //GetTypes(client);
            //GetProperties(client);
            //GetTypesAndProperties(client,"/common");

            Utils.Prompt();


        }

        //public static void GetTypes(MetawebClient client)
        //{
        //    foreach (string type in client.GetTypes("/music"))
        //    {
        //        Console.WriteLine(type);
        //    }
        //}

        //public static void GetProperties(MetawebClient client)
        //{
            
        //    foreach (string property in client.GetProperties("/type/domain"))
        //    {
        //        Console.WriteLine(property);
        //    }
        //}

        //public static void GetTypesAndProperties(MetawebClient client, string domain)
        //{
        //    foreach (string type in client.GetTypes(domain))
        //    {

        //        Console.WriteLine("type:" + type);
        //        foreach (string property in client.GetProperties(type))
        //        {
        //            Console.WriteLine(property);
        //        }
        //        Console.WriteLine();
        //    }
        //}







        private static void GetDomains(MetawebClient client)
        {

            string cursor = null;


            int i = 0;
            do
            {
                MqlBuilder b = new MqlBuilder();
                using (b.StartQueryMultiple("qname", cursor))
                {
                    b.WriteProperty("id");
                    b.WriteProperty("key");
                    b.WriteProperty("name");
                    b.WriteProperty("type", "/type/domain");
                    b.WriteProperty("limit", 1000);
                    
                }

                JsonObject r = client.MqlReadObject(b);

                cursor = r.Get<JsonObject>("qname").Get<string>("cursor");

                //Console.WriteLine(cursor);


                foreach (JsonObject result in r.Get<JsonObject>("qname").Get<JsonArray>("result"))
                {
                    
                    Trace.WriteLine(result.Get<string>("id"));
                    

                }

                i++;
            } while (cursor != false.ToString());

         

           
            Console.WriteLine(i);

           // Dump(0, r);


        }


        private static void GetFirstTenRadioheadSingles(MetawebClient client) {

            MqlBuilder b = new MqlBuilder();


            using (b.StartQueryUnique("qname"))
            {
                b.WriteProperty("type", "/music/artist");
                b.WriteProperty("name", "radiohead");
                b.WriteProperty("creator", "/user/metaweb");

                b.WritePropertyName("album");
                using (b.StartArray())
                {
                    using (b.StartObject())
                    {
                        //b.WriteProperty("*");
                        b.WriteProperty("name");
                        b.WriteProperty("release_date");
                        b.WriteProperty("release_type", "Single");

                        b.WriteProperty("index");
                        
                        b.WriteProperty("limit", 10);
                        b.WriteProperty("sort", "release_date");
                    }
                }

            }



            JsonObject r = client.MqlReadObject(b);

            Console.WriteLine("status:" + r.Get<string>("status"));


            // first name
            Console.WriteLine("qname.result.album.name:{0}",
               r.Get<JsonObject>("qname")
                .Get<JsonObject>("result")
                .Get<JsonArray>("album")
                .Get<JsonObject>(0)
                .Get<string>("name")
            );


            // all albums (release_date and name)
            foreach (JsonObject album in
                  r.Get<JsonObject>("qname")
                .Get<JsonObject>("result")
                .Get<JsonArray>("album")
                )
            {
                Console.WriteLine("result:{0} {1} {2}", 
                    album.Get<int>("index"),
                    album.Get<DateTime>("release_date").ToShortDateString(),
                    album.Get<string>("name"));
            }



            // take a look at the result structure
            Dump(0,r);
            





        }

        private static void Dump(int indent, JsonObject obj)
        {
            foreach (string propertyName in obj)
            {
                object pv = obj.Get<object>(propertyName);

                Trace.WriteLine(string.Format("{0}{1}: {2}",
                    new string('\t',indent),
                    propertyName,
                    pv
                    ));

                if (pv is JsonObject)
                {
                    Dump(indent+1,pv as JsonObject);
                }

                if (pv is JsonArray)
                {
                    JsonArray ja = pv as JsonArray;

                    foreach (object jai in ja)
                    {
                        if (jai is JsonObject)
                        {
                            Dump(indent+1, jai as JsonObject);
                        }
                    }


                }
            }

        }

    }
}
